<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Edit Offer (/admin/offer_edit.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require(dirname(__FILE__) . "/../config.php");

// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {
	/* If we're not logged in, take us to the login
	   page and ensure the rest of the code isn't run */
	header("Location: /admin/login.php");
	exit;
}

// Extra jQuery for the preview function
$extra_jQuery = "$(\"#preview\").click(function(){\n
var offer_line1 = $(\"input#offer_line1\").val();
var offer_line2 = $(\"input#offer_line2\").val();
var offer_line3 = $(\"input#offer_line3\").val();
var offer_type = $(\"select#offer_type\").val();
var offer_price = $(\"input#offer_price\").val();
var offer_value = $(\"input#offer_value\").val();
var offer_pos_code = $(\"input#offer_pos_code\").val();
var campaign_id = $(\"input#campaign_id\").val();
var offer_end_date = $(\"input#offer_end_date\").val();
var imgTag = '<img src=\\\"offer_preview.php?offer_line1=' + offer_line1 +
'&offer_line2=' + offer_line2 +
'&offer_line3=' + offer_line3 +
'&offer_type=' + offer_type +
'&offer_price=' + offer_price +
'&offer_value=' + offer_value +
'&offer_pos_code=' + offer_pos_code +
'&campaign_id=' + campaign_id +
'&offer_end_date=' + offer_end_date +
'\\\" alt=\\\"(Preview image.)\\\" />';
$(\"#preview-box\").fadeIn('slow')\n
$(\"#preview-img\").html(imgTag);\n
});\n
\n
$(\"#preview-close\").click(function(){\n
$(\"#preview-box\").fadeOut('slow')\n
});\n";

// Set page title
$pagetitle = "Edit Offer (Admin)";

$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Edit Offer";
$pagecontent = "<hr />\n";

if(!isset($_GET['cid']) || !is_numeric($_GET['cid'])) {
	// No campaign to edit offer in
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign was selected for editing an offer. Select a campaign by clicking its name on the List Campaigns page.</div>\n";
} else if(!isset($_GET['oid']) || !is_numeric($_GET['oid'])) {
	// No offer to edit
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No offer was selected for editing. Select an offer by clicking its respective \"Edit\" icon on the List Offers page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'") == false) {
	// Non-existent campaign
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not exist. Please select another offer for editing by using its respective \"Edit\" icon on the List Offers page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `campaign_id` = '" . $_GET['cid'] . "'") == false) {
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not contain any offers. Please select another campaign by clicking its name on the List Campaigns page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `offer_id` = '" . $_GET['oid'] . "'") == false) {
	// Non-existent offer
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The offer selected does not exist. Please select another offer for editing by using its respective \"Edit\" icon on the List Offers page.</div>\n";
} else {
	// Get campaign and offer information from database
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'");
	$offer = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `offer_id` = '" . $_GET['oid'] . "'");

	// Generate editing form
	$page_content .= "<h3>Offer #" . $offer->offer_id . " in campaign '" . $campaign->campaign_name . "'</h3>\n";
	$page_content .= "<form action=\"handler.offer_edit.php\" method=\"post\">\n";
	$page_content .= "<table class=\"table-60\">\n";
	$page_content .= "<input type=\"hidden\" name=\"offer_id\" value=\"" . $offer->offer_id ."\" />\n";
	$page_content .= "<input type=\"hidden\" name=\"campaign_id\" id=\"campaign_id\" value=\"" . $campaign->campaign_id ."\" />\n";
	$page_content .= "<input type=\"hidden\" name=\"offer_end_date\" id=\"offer_end_date\" value=\"" . $campaign->end_date ."\" />\n";
	$page_content .= "<tr><td><label for=\"offer_line1\">Offer Line 1<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line1\" id=\"offer_line1\" size=\"32\" maxlength=\"32\" value=\"" . $offer->offer_line1 ."\" /></td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_line2\">Offer Line 2<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line2\" id=\"offer_line2\" size=\"32\" maxlength=\"32\" value=\"" . $offer->offer_line2 ."\" /></td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_line3\">Offer Line 3<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line3\" id=\"offer_line3\" size=\"32\" maxlength=\"32\" /></td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_pos_code\">Offer POS Code<br />(4 digits max.)</label></td><td># <input type=\"text\" name=\"offer_pos_code\" id=\"offer_pos_code\" size=\"4\" maxlength=\"4\" value=\"" . $offer->offer_pos_code ."\" /></td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_type\">Offer Type</label></td><td><select name=\"offer_type\" id=\"offer_type\"><option>(Select)</option>\n";

	$offer_types = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offertypes`");
	foreach( $offer_types as $offer_type ) {
		$page_content .= "<option value=\"" . $offer_type->offer_type_id . "\"";
		if($offer->offer_type == $offer_type->offer_type_id) $page_content .= " selected";
		$page_content .= ">" . $offer_type->offer_type_id .  " - " . $offer_type->offer_type_name . "</option>\n";
	}

	$page_content .= "</select>\n";
	$page_content .= "</td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_price\">Offer Amount<br />($ or % automatically added)</label></td><td><input type=\"text\" name=\"offer_price\" id=\"offer_price\" value=\"" . $offer->offer_price ."\" /></td></tr>\n";
	$page_content .= "<tr><td><label for=\"offer_value\">Offer Value<br />(types 0, 3, and 4)</label></td><td>$ <input type=\"text\" name=\"offer_value\" id=\"offer_value\" value=\"" . $offer->offer_value ."\" /></td></tr>\n";
	$page_content .= "<tr><td class=\"centered\" colspan=\"2\"><input type=\"submit\" name=\"action\" id=\"preview\" value=\"Preview\" onClick=\"return false;\" />&nbsp;<input type=\"submit\" name=\"action\" value=\"Save\" /></td></tr>\n";
	$page_content .= "</table>\n";
	$page_content .= "</form>\n";

	$page_content .= "<div id=\"preview-box\">\n";
	$page_content .= "<h3>This is a preview of how the coupon will look in the user interface.</h3>\n";
	$page_content .= "<p>You may click \"Preview\" above to reload the preview image.</p>\n";
	$page_content .= "<p>Your changes have not been committed yet. To commit your changes, click \"Save\" above.</p>\n";
	$page_content .= "<div id=\"preview-img\"></div>\n";
	$page_content .= "<p><a href=\"javascript:return false;\" id=\"preview-close\">Close</a></p>\n";
	$page_content .= "</div>\n";
}

$page_content .= "<a href=\"/admin/list_offers.php?cid=" . $campaign->campaign_id ."\">Back</a>\n";

//Get template
require(TEMPLATE_DIR . "/index.php");
?>